package service

import (
	"encoding/json"
	"fmt"
	constant "rongyy/src/constants"
	"rongyy/src/helper"
	"rongyy/src/middlewares"
	"rongyy/src/models"
	"time"
)

type ECardService struct {
}

// 公卫，7院卡管中心用， fetch去调用https:// + ip的接口，要报错，直接报dnsname非法，不晓得扎个解决。所以从这里转一哈，艹

func (es *ECardService) GetToken(ca *models.Ca) (string, error) {
	resp, err := helper.PostJson("https://221.237.182.160:9595/internet/api/app/v1/token", ca)
	type rr struct {
		RespCode string `json:"respCode"`
		RespDesc string `json:"respDesc"`
		RespData struct {
			BizContent struct {
				Token string `json:"token"`
			} `json:"bizContent"`
		} `json:"respData"`

		//RespData string `json:"respData"`
	}
	add := &rr{}
	err = json.Unmarshal(resp, add)
	if err != nil {
		return "", err
	}
	fmt.Println(add)
	return add.RespData.BizContent.Token, nil
}

func (es *ECardService) GetTokenTest(ca *models.Ca) (string, error) {
	//resp, err := helper.PostJson("http://221.237.182.251:8585/internet/api/app/v1/token", ca)

	jsonData, err := json.Marshal(ca)
	middlewares.Logger.Println(string(jsonData))
	resp, err := helper.PostJson("https://221.237.182.160:9595/internet/api/app/v1/token", ca)
	type rr struct {
		RespCode string `json:"respCode"`
		RespDesc string `json:"respDesc"`
		RespData struct {
			BizContent struct {
				Token string `json:"token"`
			} `json:"bizContent"`
		} `json:"respData"`

		//RespData string `json:"respData"`
	}
	add := &rr{}
	err = json.Unmarshal(resp, add)
	if err != nil {
		return "", err
	}
	fmt.Println(add)
	return add.RespData.BizContent.Token, nil
}
func (uc *ECardService) GetEcard(pa *models.EcardPa) (*models.EcardResp, error) {
	resp, err := helper.PostJson("https://221.237.182.160:9595/internet/api/app/v1/ecard", pa)
	add := &models.EcardResp{}
	err = json.Unmarshal(resp, add)
	if err != nil {
		return nil, err
	}
	return add, nil
}
func (uc *ECardService) GetEcardTest(pa *models.EcardPa) (*models.EcardResp, error) {
	middlewares.Logger.Println(pa)
	resp, err := helper.PostJson("http://221.237.182.251:8585/internet/api/app/v1/ecard", pa)
	add := &models.EcardResp{}
	err = json.Unmarshal(resp, add)
	if err != nil {
		return nil, err
	}
	return add, nil
}
func (uc *ECardService) GetEcardInfo(pa *models.EcardPa) (*models.EcardResp, error) {
	fmt.Println(pa)
	resp, err := helper.PostJson("https://221.237.182.160:9595/internet/api/app/v1/ecard", pa)
	add := &models.EcardResp{}
	err = json.Unmarshal(resp, add)
	if err != nil {
		return nil, err
	}
	return add, nil
}

func (uc *ECardService) YktPay(pa *models.EcardPa) (*models.EcardResp, error) {
	fmt.Println(pa)
	resp, err := helper.PostJson("http://221.237.182.251:8585/internet/context/rest/88800200001", pa)
	add := &models.EcardResp{}
	err = json.Unmarshal(resp, add)
	if err != nil {
		return nil, err
	}
	return add, nil
}

// Create
//
//	@Description: 建电子健康卡
//	@receiver pc
//	@param req
//	@return error
func (pc *ECardService) GetQRCode(req *models.EcardCreateReq) (string, error) {
	//secret, err := helper.Sm2Encrypto(constant.CardCenterAppSecretTest, constant.CardCenterPubKeyTest)
	secret, err := helper.Sm2Encrypto(constant.CardCenterAppSecret, constant.CardCenterPubKey1)
	middlewares.Logger.Println("secret====", secret)

	type BizContent struct {
		Secret string `json:"secret"`
	}
	biz := &BizContent{Secret: secret}
	ca := &models.Ca{
		EncType:    "SM4",
		Method:     "token",
		DigestType: "SM2",
		//AppId:      constant.CardCenterAppIdTest,
		AppId:      constant.CardCenterAppId,
		BizContent: *biz,
		Version:    "v1.10",
		Timestamp:  time.Now().UnixMicro(),
	}
	token, err := (&ECardService{}).GetTokenTest(ca)
	// 不存在，就走卡管中心，建卡
	sm4Info := &models.Sm4QR{
		Vuid: req.EcardNo,
	}
	fmt.Println("3++++++++++++++++++++++++++", sm4Info)
	reqSm4 := &models.Sm4EncryptReq{
		Info:  *sm4Info,
		Token: token,
	}
	sm4resp, err := helper.Sm4Encrypto(reqSm4)
	fmt.Println("4++++++++++++++++++++++++++", string(sm4resp))
	encryToken, err := helper.Sm2Encrypto(token, constant.CardCenterPubKey1)
	if err != nil {
		middlewares.Logger.Println("卡管中心报错------", err.Error())
		return "", nil
	}
	ecardPa := &models.EcardPa{
		EncType:    "sm4",
		Method:     "apply",
		DigestType: "SM2",
		AppId:      constant.CardCenterAppId,
		BizContent: sm4resp,
		Digest:     encryToken,
		Version:    "v1.10",
		Timestamp:  time.Now().UnixMicro(),
	}
	res, err := (&ECardService{}).GetEcard(ecardPa)
	middlewares.Logger.Println(res)
	if res.RespCode != "10000" {
		return "", fmt.Errorf(res.RespDesc)
	}

	// 如果卡管中心报错，直接院内建卡
	if err != nil {
		middlewares.Logger.Println("卡管中心报错------", err.Error())
		return "", nil
	}
	EcardInfo := res.RespData.BizContent

	data, err := helper.SM4EcbDecrypt([]byte(token), EcardInfo)
	type Sm4QRModel struct {
		Vuid      string `json:"vuid"`
		Empi      string `json:"empi"`
		EcContent string `json:"ecContent"`
		Img       string `json:"img"`
	}

	sss := &Sm4QRModel{}
	err = json.Unmarshal(data, sss)
	//检查卡是否在his系统
	fmt.Println("4++++++++++++++++++++++++++", sss)
	return sss.EcContent, nil
}
